<template>
    <div class="city-search">
        <div class="search-input">
            <input type="text" placeholder="请输入城市名称" v-model="inputCity" @input="handleInput" />
            <div class="city-box" :style="{height:cityBoxHeight+'px'}" ref="cityBox">
                <div class="city-box-item">
                    <div v-if="resData">
                        <div v-for="city in resData" @click="searchWeather(city.adcode,city.city)">{{ city.city }}</div>
                    </div>
                    <p v-else>似乎没有查找到您要搜索的城市信息</p>
                </div>
            </div>
        </div>
        <div>
            <div v-if="CityItemStore.cityList.length>0" class="city-list">
                <div v-for="city in CityItemStore.cityList" :key="city.adcode" class="city-list-item">
                    <div class="city-item-data">
                        <span>{{ city.name }}</span>
                        <span>{{ city.weather.temperature }}℃</span>
                    </div>
                    <div class="city-item-btn">
                        <button @click="searchWeather(city.adcode,city.name)">查看</button>
                        <button @click="deleteCity(city.adcode)">删除</button>
                    </div>
                </div>
            </div>
            <div v-else class="city-list-empty">
            暂时没有保存过的城市天气信息，请查询后点击右上角“+”号保存
            </div>
        </div>
    </div>
</template>

<script setup>
import { useCityItemStore } from '@/stores/CityItem';
import { nextTick, ref } from 'vue';
import { getGeo } from '@/apis/city';
import { useRouter } from 'vue-router';

const CityItemStore = useCityItemStore()
const cityBoxHeight = ref(0)
const cityBox = ref('')
const inputCity = ref('')
const resData = ref('')
const router = useRouter()

const handleInput = () => {
    handleInputDebounce()
}
document.addEventListener('click', (e) => {
    if (!cityBox.value.contains(e.target)) {
        cityBoxHeight.value = 0
    } else {
        return
    }
})
const searchWeather = (adcode, city) => {
    CityItemStore.setShowCity(city, adcode)
    router.push({ name: 'about', params: { adcode } })
}
const deleteCity = (adcode) => {
    CityItemStore.removeCity(adcode)
}
function debounce (fn, delay) {
    let timer
    return function () {
        const context = this
        const args = arguments
        clearTimeout(timer)
        timer = setTimeout(() => {
            fn.apply(context, args)
        }, delay)
    }
}
const handleInputDebounce = debounce(async () => {
    await nextTick()
    cityBoxHeight.value = cityBox.value.scrollHeight
    getGeo(inputCity.value).then(res => {
        resData.value = res.data.geocodes
    })
}, 500)
</script>

<style lang="scss" scoped>
.city-search {
    padding: 0 20%;
    color: white;
    input {
    margin-top: 15px;
    height: 40px;
    width: 100%;
    outline: none;
    border: none;
    background-color: transparent;
    border-bottom: 1px solid white;
    font-size: 1.125rem;
    color: white;
    &:focus {
        box-shadow: 1px 2px 3px rgba(0,0,0,0.3);
        border-bottom: none;
    }
    &::placeholder {
    color: #999;
    }
  }
  .city-box {
    margin-top: 6px;
    transition: all 0.5s ease;
    cursor: pointer;
    background-color: rgb(0, 78, 113);
    box-shadow: 1px 1px 3px rgba(0,0,0,0.3);
    overflow: hidden;
    .city-box-item {
        font-size: 1rem;
        padding: 15px 5px;
    }
  }
  .city-list{
    margin: 40px 0;
    .city-list-item{
        display: flex;
        justify-content: space-between;
        height: 40px;
        margin: 15px 0;
        .city-item-data{
        height: 40px;
        background-color: rgb(0, 78, 113);
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0 15px;
        border-radius: 5px;
        transition: all 0.5s ease;
        span{
            display: block;
        }
    }
    .city-item-btn{
        display: none;
        transition: all 0.5s ease;
        button{
            width: 80px;
            height: 40px;
            font-size: 16px;
            text-align: center;
            border: none;
            background-color: #eab308;
            color: white;
            margin-left: 10px;
            cursor: pointer;
            border-radius: 5px;
            &:hover{
                background-color: red;
            }
        }
    }
    &:hover{
        .city-item-data{
            width: 80%;
        }
        .city-item-btn{
            display: flex;
        }
    }
    } 
  }
  .city-list-empty {
    font-size: 1rem;
    padding: 35px 0;
    color: white;
    text-align: center;
  }
}
</style>